# 项目:
# 文件: tools
# 作者: Yang
# 日期: 2025/5/22 09:11
# coding:utf-8
import json

import pytest
import requests


from selenium import webdriver

class Util:

    @classmethod
    def read_ini(cls,section):
        with open('../data/elements.ini',encoding='utf-8') as file:
            contents = file.read()
        all = contents.split('[ ')
        result = {}
        for item in all:
            if item.startswith(section):
                temp = item.split(' ]')[1].strip()
                items = temp.split('\n')
                for it in items:
                    if not it.startswith('# ') and it != '\n':
                        value = it.split(' = ')[1]
                        if '# ' in value:
                            value1 = value.split('# ')[0].strip()
                            result[it.split(' = ')[0]] = value1
                        else:
                            result[it.split(' = ')[0]] = value
                return result

    @classmethod
    def get_ini_value(cls,section,option):
        result = cls.read_ini(section)
        return eval(result[option])

    @classmethod
    def get_info(cls,casename):
        caseinfo = Util.read_json('../YDqbtest/conf.json')[casename]
        return Util.read_excel(**caseinfo)

    # sheetname,starrow,endrow,data_col,expect_col
    @classmethod
    def read_excel(cls, **kwargs):
        import xlrd
        workbook = xlrd.open_workbook('../data/testsace.xls')
        sheet = workbook.sheet_by_name(kwargs['sheetname'])
        li = []
        for i in range(kwargs['starrow'], kwargs['endrow']):
            di = {}
            api_url = sheet.cell(i, kwargs['api_url']).value  # 读取第i列的api_url
            di['api_url'] = api_url
            api_method = sheet.cell(i, kwargs['api_method']).value
            di['api_method'] = api_method
            api_data = sheet.cell(i, kwargs['api_data']).value
            temp = str(api_data).split('\n')
            for t in temp:
                di[t.split('=')[0]] = t.split('=')[1]
            status_code = sheet.cell(i, kwargs['status_code']).value
            di['status_code'] = status_code
            resp_content = sheet.cell(i, kwargs['resp_content']).value
            di['resp_content'] = resp_content
            expect = sheet.cell(i, kwargs['expect']).value
            di['expect'] = expect
            li.append(di)
        return li


    @classmethod
    def read_json(cls, path):
        """
        用于读取指定的json文件，返回json格式的数据
        :param path: json文件的路径和名称
        :return: json格式的数据
        """
        with open(path, encoding='utf-8') as file:
            contents =  json.load(file)
        return contents

    @classmethod
    def get_conn(cls):
        import pymysql
        return pymysql.connect(host='localhost', user='root',
                               password='1234', database='yunduanboss', port=3306,charset='utf8')

    @classmethod
    def query(cls, sql):
        conn = cls.get_conn()
        cursor = conn.cursor()
        cursor.execute(sql)
        result = cursor.fetchall()
        cursor.close()
        conn.close()
        return result

    @classmethod
    def update(cls, sql):
        conn = cls.get_conn()
        cursor = conn.cursor()
        cursor.execute(sql)
        conn.commit()
        cursor.close()

if __name__ == '__main__':
    pass